package com.joolun.web.api;


import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.core.domain.entity.SysUser;
import com.joolun.common.core.domain.model.LoginUser;
import com.joolun.common.core.page.TableDataInfo;
import com.joolun.common.utils.ServletUtils;
import com.joolun.common.utils.StringUtils;
import com.joolun.tennis.domain.Club;
import com.joolun.tennis.domain.WxUserClub;
import com.joolun.tennis.service.IClubService;
import com.joolun.tennis.service.IWxUserClubService;
import com.joolun.tennis.service.impl.ClubServiceImpl;
import com.joolun.weixin.utils.ThirdSessionHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/api/ma/club")
@Api(value = "club", tags = "俱乐部API")
public class ClubApi extends BaseController {

    private final IClubService clubService;

    private final IWxUserClubService wxUserClubService;

    /**
     * 通过id查询俱乐部
     * @param id
     * @return R
     */
    @ApiOperation(value = "通过id查询俱乐部")
    @GetMapping("/{id}")
    public AjaxResult getById(HttpServletRequest request, @PathVariable("id") Long id){
        Club club = clubService.selectClubById(id);
        WxUserClub wxUserClub = new WxUserClub();
        String wxUserId = ThirdSessionHolder.getWxUserId();
        wxUserClub.setWxUserId(wxUserId);
        wxUserClub.setClubId(id);
        List<WxUserClub> list = wxUserClubService.selectWxUserClubList(wxUserClub);
        club.setIsCollect(list.size());
        return AjaxResult.success(club);
    }
    /**
     * 取消收藏俱乐部接口
     *
     * @return R
     */
    @ApiOperation(value = "收藏俱乐部")
    @GetMapping("/collectClub")
    public AjaxResult collectClub(HttpServletRequest request, @RequestParam Long clubId){
        WxUserClub wxUserClub = new WxUserClub();
        wxUserClub.setClubId(clubId);
        wxUserClub.setWxUserId(ThirdSessionHolder.getWxUserId());
        if(wxUserClub.getClubId()==null||wxUserClub.getClubId()<=0) return AjaxResult.error("缺必要参数");
        return AjaxResult.success(clubService.collectClub(wxUserClub));
    }
    /**
     * 取消收藏俱乐部接口
     *
     * @return R
     */
    @ApiOperation(value = "取消收藏俱乐部")
    @GetMapping("/cancelClub")
    public AjaxResult cancelClub(HttpServletRequest request, @RequestParam Long clubId){
        WxUserClub wxUserClub = new WxUserClub();
        wxUserClub.setClubId(clubId);
        wxUserClub.setWxUserId(ThirdSessionHolder.getWxUserId());
        if(wxUserClub.getClubId()==null||wxUserClub.getClubId()<=0) return AjaxResult.error("缺必要参数");
        return AjaxResult.success(clubService.cancelClub(wxUserClub));
    }

    /**
     * 查询俱乐部列
     * 展示俱乐部名称、所在城市、LOGO、联系电话
     */
    @ApiOperation(value = "俱乐部列表")
    @GetMapping("/list")
    public TableDataInfo list(Club club)
    {
        startPage();
        List<Club> list = clubService.selectClubWxList(club);
        list.forEach(item->{
            if(!StringUtils.isEmpty(item.getPaths())) {
                String[] paths = item.getPaths().split(",");
                if (paths.length > 0) item.setPicUrls(paths);
            }
        });
        return getDataTable(list);
    }

    @ApiOperation(value = "俱乐部列表")
    @GetMapping("/collectList")
    public TableDataInfo collectList()
    {
        startPage();
        List<Club> list = clubService.selectCollectClubList(ThirdSessionHolder.getWxUserId());
        list.forEach(item->{
            if(!StringUtils.isEmpty(item.getPaths())) {
                String[] paths = item.getPaths().split(",");
                if (paths.length > 0) item.setPicUrls(paths);
            }
        });
        return getDataTable(list);
    }
}
